package com.jobsleaf.delegate.app.event;
import java.text.DateFormat;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;


public class ReceiverView extends ViewPart {
	  private TableViewer viewer;
	 
	  @Override
  public void createPartControl(final Composite parent) {
    parent.setLayout(new FillLayout());
    viewer = new TableViewer(parent);
    viewer.getTable().setHeaderVisible(true);
    viewer.getTable().setLinesVisible(true);
    viewer.setLabelProvider(new ColumnLabelProvider() {
      
      public String getText(Object element) {
        return DateFormat.getDateTimeInstance().format(element);
	      }
    });
	 
	    BundleContext ctx = FrameworkUtil.getBundle(ReceiverView.class).getBundleContext();
    EventHandler handler = new EventHandler() {
	      public void handleEvent(final Event event) {
	        if( parent.getDisplay().getThread() == Thread.currentThread() ) {
	          viewer.add(event.getProperty("DATA"));
	        } else {
	          parent.getDisplay().syncExec(new Runnable() {
	            public void run() {
              viewer.add(event.getProperty("DATA"));
            }
	          });
        }
	      }
    };
	 
    Dictionary<String,String> properties = new Hashtable<String, String>();
    properties.put(EventConstants.EVENT_TOPIC, "viewcommunication/*");
    ctx.registerService(EventHandler.class.getName(), handler, properties);
	  }
	 
	  @Override
	  public void setFocus() {
	    viewer.getTable().setFocus();
	  }
	}